// 表单验证规则方法

import i18n from '../modules/i18n'

interface Irules {
  required(trigger: string): any,

  inputLengthLimit(num: number, trigger: string): any,

  mustNumber(trigger: string): any
}

class BfRules implements Irules {
  required(trigger = "blur") {
    return {
      required: true,
      message: i18n.t("formRules.required"),
      trigger: trigger
    }
  }

  inputLengthLimit(num = 16, trigger = 'blur') {
    return {
      max: num,
      message: i18n.t("formRules.limitCharacter", {num}),
      trigger: trigger
    }
  }

  mustNumber(trigger = 'blur') {
    return {
      validator: (rule, value, callback) => {
        let reg = /^\d+$/;
        if (value !== "" && !reg.test(value)) {
          let err: any = i18n.t("formRules.mustNumber");
          callback(new Error(err));
        } else {
          callback();
        }
      }, trigger: trigger
    }
  }
}

export default new BfRules();
